package jvs.vfs.resource;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import jvs.vfs.IFileBaseImpl;
import x.java.io.File;

/**
 * @author qiangli
 * 
 */
public class UrlFileImpl extends IFileBaseImpl {
	private String url = null;
	public UrlFileImpl(URI uri) {
		super(uri);
		// url:<url>
		url = uri.toString();
		url = url.substring(4).trim();
	}

	public boolean create() {
		return false;
	}

	public boolean delete() {
		return false;
	}

	//private String getPath() {
	//	return path;
	//}

	public boolean exists() {
		return true;
	}

	public InputStream getInputStream() {
		try {
			URL u = new URL(url);
			return u.openStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public OutputStream getOutputStream() {

		return null;
	}

	public boolean isDirectory() {
		return url.endsWith("/");
	}

	private String[] list = null;
	
	private String[] read(String path) throws Exception {
		final String[] names = {"index.txt","","index.html","index.htm"};
		String[] list = null;
		for (int i=0; i<names.length; i++) {
			URL url = new URL(path + names[i]);
			
			try {
				BufferedReader in = new BufferedReader(new InputStreamReader(url
						.openStream()),File.BUFSIZE);
				StringBuffer sb = new StringBuffer();
				String s = null;
				while ((s = in.readLine()) != null) {
					s = s.trim();
					if (s.length() > 0) {
						sb.append(s+"\n");
					}
				}
				in.close();
				//cheap test
				if (sb.indexOf("<") != -1 && sb.indexOf(">") != -1) {
					//html
					List<URL> links = LinkExtractor.scan(url, sb.toString());
					//
					HashSet<String> set = new HashSet<String>();
					
					int prefixLen = path.length();
					for (Iterator<URL> it=links.iterator(); it.hasNext();) {
						String link = it.next().toString();
						if (!link.startsWith(path)) {
							continue;
						}
						link = link.substring(prefixLen);
						int idx = link.indexOf("/");
						int idxq = link.indexOf("?");
						
						if (idx > 0 && (idxq == -1 || idx < idxq)) {
							set.add(link.substring(0,idx+1));
						} else {
							set.add(link);
						}
					}
					list = (String[])set.toArray(new String[0]);
				} else {
					//text
					list = sb.toString().split("\n");
				}
				return list;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				continue;
			}
		}
		return new String[0];
	}
	
	synchronized public String[] list() {
		if (list == null) {
			try {
				list = read(url);
			} catch (Exception e) {
				e.printStackTrace();
				list = new String[0];
			}
		}
		return list;
	}

	public boolean mkdir() {
		return false;
	}

	public boolean move(URI uri) {
		return false;
	}
}
